﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
using RollcallClient.Localhost;
using System.Windows.Forms;
using System.Threading;
using System.Collections.ObjectModel;

namespace RollCallClient
{

    //Hold information of User
    class UserInfo
    {
        //Name of User
        String strUserName;
        //MAC address of User
        String strMac;
        //List of days that user choose to check attendence
        private Collection<int> clDates = new Collection<int>();
        /// <summary>
        /// Information of user
        /// </summary>
        public UserInfo()
        {
            strUserName = ""; // Not assign anything for username    

            //Get Mac address of current machine and add it
            //to MAC address of user
            strMac = GetCurrentMACAddress();
        }
        /// <summary>
        /// Information of user
        /// </summary>
        /// <param name="user"></param>
        public UserInfo(String strUserName)
        {
            this.strUserName = strUserName;
            strMac = GetCurrentMACAddress();
        }
        /// <summary>
        /// Contructor have to parameter
        /// </summary>
        /// <param name="user"></param>
        /// <param name="mac"></param>
        public UserInfo(String strUser, String strMac)
        {
            //add string to username
            this.strUserName = strUser;
            //add string mac to MAC
            this.strMac = strMac;
        }
        /// <summary>
        /// Set Day that user choose to check attendance
        /// </summary>
        /// <param name="day"></param>
        public void SetDayChecking(Collection<int> clDates)
        {
            this.clDates = clDates;
        }
        /// <summary>
        /// Set username value (from the textbox) for checking 
        /// </summary>
        /// <param name="user"></param>
        public void SetUserName(String user)
        {
            this.strUserName = user;
        }
        /// <summary>
        /// Set mac address value for checking
        /// </summary>
        /// <param name="mac"></param>
        public void SetMACAddress(String mac)
        {
            this.strMac = mac;
        }
        
        /// <summary>
        /// Get list of date that choose
        /// </summary>
        /// <returns></returns>
        public Collection<int> GetdayChecking()
        {
            return clDates;
        }
        /// <summary>
        /// Get username
        /// </summary>
        /// <returns></returns>
        public String GetUserName()
        {
            return this.strUserName;
        }
        /// <summary>
        /// Get Mac address
        /// </summary>
        /// <returns></returns>
        public String GetMACAddress()
        {
            return this.strMac;
        }
        /// <summary>
        /// Return the Mac address of local machine
        /// </summary>
        /// <returns></returns>
        public String GetCurrentMACAddress()
        {
            // Browse all network interface to return right mac address

            //return Mac address when connected by Wifi
            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {   //Gets the current operational state of the network connection.
                //and check the network interface is up; it can transmit data packets.
                if (ni.OperationalStatus == OperationalStatus.Up &&
                    //and check if the network interface is unable to transmit data packets because of a missing
                    //component, typically a hardware component.
                        ni.OperationalStatus != OperationalStatus.NotPresent &&
                    //Check if the network interface uses a tunnel connection.
                    //if yes, we can't get Mac address
                            ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel &&
                    //Check if the network interfacs is a loopback adapter.
                    //if yes, it's just for testing; no traffic is sent.
                                ni.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
                    //The network interface uses a wireless LAN connection (IEEE 802.11 standard).
                                        ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    //return mac address
                    return ni.GetPhysicalAddress().ToString();
                }
                //return mac address when connected by Ethernet
                if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
                    //Gets the current operational state of the network connection.
                    //and check the network interface is up; it can transmit data packets.
                        ni.OperationalStatus == OperationalStatus.Up)
                {
                    //return mac address
                    return ni.GetPhysicalAddress().ToString();
                }
            }
            //if there's no mac address, return NULL
            return null;
        }
        /// <summary>
        /// Check if Connection is available
        /// </summary>
        /// <returns></returns>
        public bool IsInternetConnected()
        {

            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                //Gets the current operational state of the network connection.
                //and check the network interface is up; it can transmit data packets. 
                if (ni.OperationalStatus == OperationalStatus.Up &&
                    //and check if the network interface is unable to transmit data packets because of a missing
                    //component, typically a hardware component.
                                ni.OperationalStatus != OperationalStatus.NotPresent &&
                    //Check if the network interface uses a tunnel connection.
                    //if yes, we can't get Mac address
                                        ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel &&
                    //Check if the network interfacs is a loopback adapter.
                    //if yes, it's just for testing; no traffic is sent.
                                                ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
                {
                    //return that Connection is available
                    return true;
                }
            }
            //return connection is not available
            return false;
        }
        /// <summary>
        /// Return result if writing configuration file is OK
        /// </summary>
        /// <returns></returns>
        public bool WriteUserInfoToFile()
        {
            //assign result 'True' show that WriteUserInfo is OK
            bool bResult = true;
            //Implements a System.IO.TextWriter for writing characters to a stream in a
            //particular encoding and assgin it to NULL
            StreamWriter swFileWriter = null;
            try
            {
                //Writing file userInfo.dat to special folder \\Mydocument\RollcallClient
                swFileWriter = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                            + "\\RollCallClient\\UserInfo.dat");
                //Writing Username into this file
                swFileWriter.WriteLine(this.strUserName);
                //Writing the days that user choose in form InfoSeting to file 
                for (int i = 0; i < clDates.Count; i++)
                {
                    swFileWriter.Write(clDates[i] + ";");
                }

            }
            catch
            {
                //Set result 'False' show that can't create file or writing file.
                bResult = false;
            }
            finally
            {
                //Close TextWriter
                swFileWriter.Close();
            }
            //return result
            return bResult;
        }
        /// <summary>
        /// return the result if Read information of user is OK
        /// </summary>
        /// <returns></returns>
        public bool ReadUserInfoFromFile()
        {
            //hold value that been read from line 1 in UserInfo.dat (For Username) 
            String strFirstLine;
            //Hold value that been read from line 2 in UserInfo.dat (For Password)
            String strSecondLine;
            //set default value is true, that reading is OK
            bool bResult = true;
            //Checking if file is not exist 
            if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\RollCallClient\\UserInfo.dat"))
            {
                //Implements a System.IO.TextReader that reads characters from a byte stream
                //in a particular encoding and assign it reading UserInfo.dat
                StreamReader srFileReader = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\RollCallClient\\UserInfo.dat");
                try
                {
                    //Read username and assign it to 'line 1'
                    strFirstLine = srFileReader.ReadLine();
                    //Read dates was checked and assign it to 'line 2' 
                    strSecondLine = srFileReader.ReadLine();
                    //check if length of username = 0 
                    if (strFirstLine.Trim().Length == 0)
                    {
                        //if username string is empty, return false show that it can't read file
                        return false;
                    }
                    //if line 1 is not emply or not = ''
                    else
                    {
                        //fill username string with 'line 1 content'
                        this.strUserName = strFirstLine;
                    }
                    //read the date that's checked by user 
                    //check if length of line2 is empty or '' 
                    if (strSecondLine.Trim().Length != 0)
                    {
                        //used for save values that had been read from file
                        string[] strDates = new string[7];
                        //read each value that separate by ';'
                        strDates = strSecondLine.Split(';');
                        //get each value of array and add it to dayChecking List
                        for (int i = 0; i < strDates.Length; i++)
                        {
                            //if there's at least one item 
                            if (strDates[i] != null)
                            {
                                try
                                {   //add day to dayChecking List
                                    this.clDates.Add(Convert.ToInt32(strDates[i]));
                                }
                                catch
                                {
                                    
                                }
                            }
                        }
                        //if there's no dayChecking was set
                        if (this.clDates == null)
                        {
                            //show that can't read file 
                            bResult = false;
                        }
                    }

                }
                catch
                {
                    //Can't read file
                    bResult = false;
                }
                finally
                {
                    //Close TextReader
                    srFileReader.Close();
                }
            }
            // if file UserInfo.dat is not exist
            else
            {
                //Create a folder named RollCallClient
                Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Personal)
                    + "\\RollCallClient");
                //set result with 'False'
                bResult = false;
            }
            //Return value show that if file is readable 
            return bResult;
        }

        /// <summary>
        /// check if today was checked or not
        /// </summary>
        /// <returns></returns>
        public bool CheckDay()
        {   //get current day and parse it to 'Int' type
            int intCurrentDate = (int)DateTime.Now.DayOfWeek;
            foreach (int intDate in clDates)
            {
                //if today was check
                if (intDate == intCurrentDate)
                {
                    //return true 
                    return true;
                }
            }
            //return false show that today wasnot check
            return false;
        }
    }
}
